package com.app.mp3player.lrc;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.PatternMatcher;

public class LrcProcessor {
	public ArrayList<Object> process(InputStream inputStream){
		//存放时间点数据
		Queue<Long> timeMills = new LinkedList<Long>();
		//存放时间点所对应的歌词
		List<String> messages = new LinkedList<String>();
		
		ArrayList<Object> queues = new ArrayList<Object>();
		
		try{
			
			InputStreamReader streamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(streamReader);
			
			String temp = "";
			int i=0;
			
			Pattern pattern = Pattern.compile("\\[([^\\]]+)\\]");
			boolean b = false;
			
			while ((temp = bufferedReader.readLine())!=null) {
				i++;
				Matcher m = pattern.matcher(temp);
				if(m.find()){
					String timeStr = m.group();
					Long timeMill = time2Long(timeStr.substring(1, timeStr.length()-1));
					if(!b){
						timeMills.offer(timeMill);
					}
					String msg = temp.substring(10);
					messages.add(msg);
				}else{
					messages.add("");
				}
			}
			queues.add(timeMills);
			queues.add(messages);
		}catch(Exception e){
			e.printStackTrace();
		}
		return queues;
	}
	
	/**
	 * 将分钟，秒全部转换成毫秒
	 * @param timeStr
	 * @return
	 */
	public Long time2Long(String timeStr) {
		String s[] = timeStr.split(":");
		int min = Integer.parseInt(s[0]);
		String ss[] = s[1].split("\\.");
		int sec = Integer.parseInt(ss[0]);
		int mill = Integer.parseInt(ss[1]);
		return min * 60 * 1000 + sec * 1000 + mill * 10L;
	}
}
